package milu.servlets;

import java.io.IOException;
import java.io.InputStream;

import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import org.apache.commons.fileupload.FileUploadException;
import com.google.appengine.api.datastore.Blob;

import javax.jdo.Query;
import org.apache.commons.io.IOUtils;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import javax.jdo.PersistenceManager;

import milu.models.*;
import milu.*;

public class Search extends HttpServlet {
    public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

        String q = request.getParameter("q");
        q = q.toLowerCase();
        PersistenceManager pm = Datastore.getPersistenceManager();

        Query query = pm.newQuery(Recipe.class, "this.tags == tag");
        query.declareParameters("String tag");
        List<Recipe> recipes = (List<Recipe>)query.execute(q);

        LOG.info("recipes: "+recipes);
        request.setAttribute("recipes", recipes);

        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
